from datetime import datetime
from app.extensions import db

class Content(db.Model):
    """学习内容表模型"""
    __tablename__ = 'content'
    
    content_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='内容唯一标识')
    type = db.Column(db.Enum('字词', '诗词', '英文', '其它',name='content_types'), 
                    nullable=False, comment='内容类型')
    title = db.Column(db.String(100), nullable=False, comment='标题')
    text = db.Column(db.Text, nullable=False, comment='内容文本')
    pinyin = db.Column(db.Text, nullable=True, comment='拼音标注')
    translation = db.Column(db.Text, nullable=True, comment='翻译文本')
    difficulty = db.Column(db.Enum('1', '2', '3', '4', '5', name='difficulty_levels'),
                         nullable=False, comment='难度等级')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    
    # 逻辑外键字段
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), 
                       nullable=False, comment='创建者ID')

    # 关系定义
    tasks = db.relationship('Task', backref='content', lazy='dynamic')
    review_plans = db.relationship('ReviewPlan', backref='content', lazy='dynamic')
    study_records = db.relationship('StudyRecord', backref='content', lazy='dynamic')

    def to_dict(self):
        return {
            'content_id': self.content_id,
            'type': self.type,
            'title': self.title,
            'text': self.text,
            'pinyin': self.pinyin,
            'translation': self.translation,
            'difficulty': self.difficulty,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'user_id': self.user_id
        }
    def __init__(self, type, title, text, pinyin, translation, difficulty, user_id):
        self.type = type
        self.title = title
        self.text = text
        self.pinyin = pinyin
        self.translation = translation
        self.difficulty = difficulty
        self.user_id = user_id
    def __repr__(self):
        return f'<Content {self.title} ({self.type})>'